package View;

import java.awt.*;

import javax.swing.*;

import Game.*;

public class BoardPanel extends JPanel {

	public BoardPanel(Pieces p) {
		super();
		pieces = p;
		dimX = Pieces.dimX;
		dimY = Pieces.dimY;
		sizeX = gridW * (dimX - 1) + leftX + rightX;
		sizeY = gridH * (dimY - 1) + leftY + rightY;
		this.setPreferredSize(new Dimension(sizeX, sizeY));
	}

	public void paint(Graphics g) {
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setColor(Color.gray);
		if (hasWon) {
			g2d.setColor(Color.blue);
		}
		g2d.fill(this.getBounds());
		g2d.setColor(Color.black);
		for (int i = 0; i < dimX; i++) {
			g2d.drawLine(i * gridW + leftX, leftY, i * gridW + leftX, gridH
					* (dimY - 1) + leftY);
		}

		for (int i = 0; i < dimY; i++) {
			g2d.drawLine(leftX, i * gridH + leftY, (dimX - 1) * gridW + leftX,
					gridH * i + leftY);
		}

		drawPieces(g2d);

	}

	public void drawPieces(Graphics2D g2d) {
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		for (int i = 0; i < dimX; i++) {
			for (int j = 0; j < dimY; j++) {
				int p = pieces.getOnePiece(i, j);
				switch (p) {
				case Pieces.BLACK:
					g2d.setColor(Color.BLACK);
					g2d.fillOval(i * gridW + leftX - gridW / 2, j * gridH
							+ leftY - gridH / 2, gridW, gridH);
					break;
				case Pieces.WHITE:
					g2d.setColor(Color.white);
					g2d.fillOval(i * gridW + leftX - gridW / 2, j * gridH
							+ leftY - gridH / 2, gridW, gridH);
					break;
				}

			}
		}
		if (gc.step>0) {
			g2d.setColor(Color.RED);
			g2d.drawOval(pieces.lastX * gridW + leftX - gridW / 2, pieces.lastY
					* gridH + leftY - gridH / 2, gridW, gridH);
		}
	}

	public void init() {
		hasWon = false;
	}

	public static final int gridW = 40;
	public static final int gridH = 40;
	private final int dimX;
	private final int dimY;

	public final int sizeX;
	public final int sizeY;

	public static final int leftX = gridW;
	public static final int leftY = gridH;

	private final int rightX = gridW;
	private final int rightY = gridH;

	public boolean hasWon = false;
	public GameController gc;
	
	private Pieces pieces;
}
